<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改)  工序节点-->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="工艺路线Id" prop="processRouteId">
          <el-input v-model="formData.processRouteId" placeholder="请输入工艺路线Id"/>
        </el-form-item>
        <el-form-item label="工序节点名称" prop="processNodeName">
          <el-input v-model="formData.processNodeName" placeholder="请输入工序节点名称"/>
        </el-form-item>
        <el-form-item label="工序Id" prop="procedureId">
          <el-input v-model="formData.procedureId" placeholder="请输入工序Id"/>
        </el-form-item>
        <el-form-item label="工序编号" prop="procedureCode">
          <el-input v-model="formData.procedureCode" placeholder="请输入工序编号"/>
        </el-form-item>
        <el-form-item label="工序名称" prop="procedureName">
          <el-input v-model="formData.procedureName" placeholder="请输入工序名称"/>
        </el-form-item>
        <el-form-item label="是否追溯" prop="isTraceabilityCode">
          <el-radio-group v-model="formData.isTraceabilityCode">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="追溯规则编号" prop="traceCodeRulesCode">
          <el-input v-model="formData.traceCodeRulesCode" placeholder="请输入追溯规则编号"/>
        </el-form-item>
        <el-form-item label="追溯规则名称" prop="traceCodeRulesName">
          <el-input v-model="formData.traceCodeRulesName" placeholder="请输入追溯规则名称"/>
        </el-form-item>
        <el-form-item label="是否委外" prop="isOutsourcing">
          <el-radio-group v-model="formData.isOutsourcing">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="这个工序是否需要绑定工装" prop="isTooling">
          <el-radio-group v-model="formData.isTooling">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序字段" prop="sort">
          <el-input v-model="formData.sort" placeholder="请输入排序字段"/>
        </el-form-item>
        <el-form-item label="库位Id" prop="storageId">
          <el-input v-model="formData.storageId" placeholder="请输入库位Id"/>
        </el-form-item>
        <el-form-item label="库位编号" prop="storageName">
          <el-input v-model="formData.storageName" placeholder="请输入库位编号"/>
        </el-form-item>
        <el-form-item label="良率" prop="passRate">
          <el-input v-model="formData.passRate" placeholder="请输入良率"/>
        </el-form-item>
        <el-form-item label="首件检" prop="firstCheck">
          <el-radio-group v-model="formData.firstCheck">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="末件检" prop="lastCheck">
          <el-radio-group v-model="formData.lastCheck">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="完工检" prop="finishedCheck">
          <el-radio-group v-model="formData.finishedCheck">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="巡检" prop="scheduleCheck">
          <el-radio-group v-model="formData.scheduleCheck">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as ProcessRouteProcessNodeApi from '@/api/basic/processrouteprocessnode';

export default {
  name: "ProcessRouteProcessNodeForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        processRouteId: undefined,
        processNodeName: undefined,
        procedureId: undefined,
        procedureCode: undefined,
        procedureName: undefined,
        isTraceabilityCode: undefined,
        traceCodeRulesCode: undefined,
        traceCodeRulesName: undefined,
        isOutsourcing: undefined,
        isTooling: undefined,
        sort: undefined,
        storageId: undefined,
        storageName: undefined,
        passRate: undefined,
        firstCheck: undefined,
        lastCheck: undefined,
        finishedCheck: undefined,
        scheduleCheck: undefined,
      },
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await ProcessRouteProcessNodeApi.getProcessRouteProcessNode(id);
          this.formData = res.data;
          this.title = "修改工序节点";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增工序节点";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await ProcessRouteProcessNodeApi.updateProcessRouteProcessNode(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await ProcessRouteProcessNodeApi.createProcessRouteProcessNode(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        processRouteId: undefined,
        processNodeName: undefined,
        procedureId: undefined,
        procedureCode: undefined,
        procedureName: undefined,
        isTraceabilityCode: undefined,
        traceCodeRulesCode: undefined,
        traceCodeRulesName: undefined,
        isOutsourcing: undefined,
        isTooling: undefined,
        sort: undefined,
        storageId: undefined,
        storageName: undefined,
        passRate: undefined,
        firstCheck: undefined,
        lastCheck: undefined,
        finishedCheck: undefined,
        scheduleCheck: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
